package com.lykj.llw.box.ui.diafrg.agent

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.KeyboardUtils
import com.lykj.lib_base.base.BaseDiaFrg
import com.lykj.lib_base.bus.FlowBus
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.databinding.DiafrgAgentVoucherGiftBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 代理代金券赠送弹窗
 * ================================================
 *
 * @author gosha
 * @date 2023/11/2-11:13
 */
class AgentVoucherGiftDiaFrg : BaseDiaFrg() {
    lateinit var binding: DiafrgAgentVoucherGiftBinding

    private var mCouponId = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DiafrgAgentVoucherGiftBinding.inflate(layoutInflater)
        initUI()
        initData()
        setListener()
        return binding.root
    }

    override fun onStart() {
        super.onStart()

        val window = dialog!!.window
        //设置窗口弹出动画
        window!!.setWindowAnimations(0)
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val layoutParams = window.attributes
        layoutParams.apply {
            dimAmount = 0.5f
            gravity = Gravity.CENTER
            width = WindowManager.LayoutParams.MATCH_PARENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
        }

        if (null != dialog) {
            val dm = DisplayMetrics()
            requireActivity().windowManager.defaultDisplay.getMetrics(dm)
            dialog?.apply {
                setCanceledOnTouchOutside(true)
                window.setGravity(Gravity.CENTER)
            }
        }
    }

    open fun setCouponId(id: Int): AgentVoucherGiftDiaFrg {
        this.mCouponId = id
        return this
    }

    override fun initUI() {
    }

    //赠送代金券
    private fun voucherGift() {
        clearBodyParam()
        initBodyParam()
        bodyParams["username"] = binding.etName.text.toString()
        bodyParams["couoponId"] = mCouponId
        RetrofitManager.instance.voucherGift(bodyParams,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@AgentVoucherGiftDiaFrg, d)
                }

                override fun specificHandle(responseBody: String) {
                    ToastUtils.showShort(requireContext(), "赠送成功")
                    FlowBus.with<String>(BConstants.BUS_KEY.GIFT_SUCCESS)
                        .post(requireActivity().lifecycleScope, "")
                    dismiss()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(requireContext(), it)
                    }
                }
            })
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.tvBtnConfirm.clickWithTrigger(350) {
            //赠送
            voucherGift()
        }

        KeyboardUtils.registerSoftInputChangedListener(requireActivity()) {
            if (it == 0) {
                binding.etName.clearFocus()
            }
        }
    }

    override fun onDestroy() {
        removeDisposables(this@AgentVoucherGiftDiaFrg)
        super.onDestroy()
    }
}